import {
  createRouter,
  createWebHashHistory
} from "vue-router";
import {
  Toast
} from 'vant';


const home = () => import("../layout/module/home/index.vue")
const shelf = () => import("../layout/module/shelf/index.vue")
const mine = () => import("../layout/module/mine/index.vue")
const hear = () => import("../layout/module/hear/index.vue")
const detail = () => import("../views/detail/detail.vue")
const balance = () => import("../layout/module/mine/balance.vue")
const order = () => import("../layout/module/mine/order.vue")
const shoppingcar = () => import("../layout/module/mine/shoppingcar.vue")
const adress = () => import("../layout/module/mine/address.vue")
const vip = () => import("../layout/module/mine/vip.vue")
const mynote = () => import("../layout/module/mine/mynote.vue")
const medal = () => import("../layout/module/mine/medal.vue")
const history = () => import("../layout/module/mine/history.vue")
const follow = () => import("../layout/module/mine/follow.vue")
const ranking = () => import("../layout/module/mine/ranking.vue")
const read = () => import("../views/read/read.vue")
const login = () => import("../views/account/login.vue")
const register = () => import("../views/account/register.vue")
const setting = () => import("../views/setting/index.vue")
const center = () => import("../views/center/index.vue")
const search = () => import("../views/search/index.vue")

const routes = [{
    path: "/",
    redirect: "/shelf"
  },
  {
    path: "/home",
    name: "home",
    component: home
  },
  {
    path: "/register",
    name: "register",
    component: register
  },
  {
    path: "/login",
    name: "login",
    component: login
  },
  {
    path: "/search",
    name: "search",
    component: search
  },
  {
    path: "/shelf",
    name: "shelf",
    component: shelf
  },
  {
    path: "/detail/:id",
    name: "detail",
    component: detail
  },
  {
    path: "/mine",
    name: "mine",
    component: mine,
  },
  {
    path: "/hear",
    name: "hear",
    component: hear
  },
  {
    path: "/balance",
    name: "balance",
    component: balance
  },
  {
    path: "/order",
    name: "order",
    component: order
  },
  {
    path: "/shoppingcar",
    name: "shoppingcar",
    component: shoppingcar
  },
  {
    path: "/address",
    name: "address",
    component: adress
  },
  {
    path: "/vip",
    name: "vip",
    component: vip
  },
  {
    path: "/mynote",
    name: "mynote",
    component: mynote
  },
  {
    path: "/medal",
    name: "medal",
    component: medal
  },
  {
    path: "/history",
    name: "history",
    component: history
  },
  {
    path: "/follow",
    name: "follow",
    component: follow
  },
  {
    path: "/ranking",
    name: "ranking",
    component: ranking
  },
  {
    path: "/read",
    name: "read",
    component: read,
    props: true
  },
  {
    path: "/setting",
    name: "setting",
    component: setting
  },
  {
    path: "/center",
    name: "center",
    component: center
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})


router.beforeEach((to, from, next) => {
  console.log('登录检查');
  // 检 测 登 录
  if (!to.fullPath.includes('login') && !sessionStorage.getItem('USER_TOKEN')) {
    Toast.fail('Token过期\n请重新登录')
    next({
      path: '/login'
    })
  } else {
    next()
  }
})
export default router
